

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.comet.CometEvent;
import org.apache.catalina.comet.CometEvent.EventType;
import org.apache.catalina.comet.CometProcessor;

/**
 * Servlet implementation class TestServlet
 */
@WebServlet("/ConnectionServlet")
public class ConnectionServlet extends HttpServlet implements CometProcessor {

	 
	    
	    public void init() throws ServletException {
	        MessageSender messageSender = MessageSender.getInstance();
	        Thread messageSenderThread = 
	            new Thread(messageSender, "MessageSender[" + getServletContext().getContextPath() + "]");
	        messageSenderThread.setDaemon(true);
	        messageSenderThread.start();
	    }

	    public void destroy() {
	        ConnectionsHolder.getInstance().connections.clear();
	        MessageSender.getInstance().stop();
	        
	    }

	    /**
	     * Process the given Comet event.
	     * 
	     * @param event The Comet event that will be processed
	     * @throws IOException
	     * @throws ServletException
	     */
	    public void event(CometEvent event)
	        throws IOException, ServletException {
	    	System.out.println(event.getEventType().toString());
	        HttpServletRequest request = event.getHttpServletRequest();
	        HttpServletResponse response = event.getHttpServletResponse();
	       
	        if (event.getEventType() == CometEvent.EventType.BEGIN) {
	            synchronized(ConnectionsHolder.getInstance().connections) {
	            	System.out.println("added connection");
	           // 	ConnectionsHolder.getInstance().connections.add(response);
	            }
	        } else if (event.getEventType() == CometEvent.EventType.ERROR) {
	          /*
	            synchronized(ConnectionsHolder.getInstance().connections) {
	            	ConnectionsHolder.getInstance().connections.remove(response);
	            }
	            event.close();*/
	        } else if (event.getEventType() == CometEvent.EventType.END) {
	            synchronized(ConnectionsHolder.getInstance().connections) {
	            	ConnectionsHolder.getInstance().connections.remove(response);
	            }
	            
	            event.close();
	        } else if (event.getEventType() == CometEvent.EventType.READ) {
	            /*InputStream is = request.getInputStream();
	            byte[] buf = new byte[512];
	            do {
	                int n = is.read(buf); //can throw an IOException
	                if (n > 0) {
	                    log("Read " + n + " bytes: " + new String(buf, 0, n) 
	                            + " for session: " + request.getSession(true).getId());
	                } else if (n < 0) {
	                    //error(event, request, response);
	                    return;
	                }
	            } while (is.available() > 0);
	       */ }
	    }



}
